/**
 * <strong>Title : EquipmentOutputController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.em.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015年11月20日 上午9:46:15<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.em.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.em.model.EquipmentOutput;
import cn.b.sky.mes.em.model.condition.EquipmentOutputCondition;
import cn.b.sky.mes.em.service.EquipmentOutputService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : EquipmentOutputController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.em.controller<br></strong>
 * <strong>Description : </strong>设备产量<br> 
 * <strong>Create on : 2015年11月20日 上午9:46:15<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/equipmentOutput/*")
public class EquipmentOutputController extends SecurityController {
  @Resource
  EquipmentOutputService equipmentOutputService;

  @RequestMapping("equipmentOutput_list")
  public String selectEquipmentOutputListByCondition(String navTabId, Model model,
      @ModelAttribute("condition") EquipmentOutputCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<EquipmentOutput> list = equipmentOutputService.selectEquipmentOutputListByCondition(condition);
    int count = equipmentOutputService.selectEquipmentOutputCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("page", condition);
    return "em/equipmentOutput/equipmentOutput_list";
  }

  @RequestMapping("addEquipmentOutput")
  public String addEquipmentOutput(String navTabId, Model model, EquipmentOutput output) {
    model.addAttribute(NAVTABID, navTabId);
    return "em/equipmentOutput/equipmentOutput_add";
  }

  @RequestMapping("modifyEquipmentOutput")
  public String modifyEquipmentOutput(String navTabId, Model model, String id) {
    EquipmentOutput output = equipmentOutputService.getEquipmentOutput(Long.valueOf(id));
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "modify");
    model.addAttribute("equipmentOutput", output);
    return "em/equipmentOutput/equipmentOutput_add";
  }

  @RequestMapping("viewEquipmentOutput")
  public String viewEquipmentOutput(String navTabId, Model model, String id) {
    EquipmentOutput output = equipmentOutputService.getEquipmentOutput(Long.valueOf(id));
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "view");
    model.addAttribute("equipmentOutput", output);
    return "em/equipmentOutput/equipmentOutput_view";
  }

  @RequestMapping("saveEquipmentOutput")
  @ResponseBody
  public DWZResult saveEquipmentOutput(String navTabId, Model model, String id, EquipmentOutput output) {
    if (output == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    output.setSiteCode(getSiteCode());
    setCreateInfo(output);
    if (StringUtils.isBlank(id)) {
      equipmentOutputService.insertEquipmentOutput(output);
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          navTabId, null, null, null);
    } else {
      equipmentOutputService.updateEquipmentOutput(output);
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          navTabId, null, null, null);
    }

  }

  @RequestMapping(value = "deleteEquipmentOutput")
  @ResponseBody
  public DWZResult deleteEquipmentOutput(Model model, String navTabId, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isEmpty(ids)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String[] idArry = ids.split(",");
    int len = idArry.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    BatchJobReport[] reports = new BatchJobReport[len];
    int failureCount = 0;
    for (int i = 0; i < idArry.length; i++) {
      reports[i] = equipmentOutputService.deleteEquipmentOutPut(Long.valueOf(idArry[i]));
      if (!reports[i].isSuccess()) {
        failureCount += 1;
      }
    }
    if (failureCount == 0) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }

  }

}
